package com.payment.factory;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

import com.basic.service.contract.IBaseService;
import com.payment.IPaymentBehavior;
import com.payment.impl.NormalPay;
import com.payment.impl.RechargePay;

public class PayFactory {

	private static Map<Integer, Class<?>> defaultPayMap = new HashMap<Integer, Class<?>>();

	static {
		// 默认
		defaultPayMap.put(0, NormalPay.class);

		defaultPayMap.put(1, RechargePay.class);
	}

	private static final Map<Integer, IPaymentBehavior> BEHAVIOR = new HashMap<Integer, IPaymentBehavior>();

	public static <T extends IPaymentBehavior> void addOperationClass(int type, T operation) {
		BEHAVIOR.put(type, operation);
	}

	/**
	 * 获取动态处理对象
	 * @param <T>
	 * @param type
	 * @param baseService
	 * @return
	 */
	public static <T extends IPaymentBehavior> IPaymentBehavior getTrendsClass(int type) {
		return BEHAVIOR.get(type);
	}

	/**
	 * 获取默认处理类
	 * 
	 * @param <T>
	 * @param <E>
	 * @param type
	 * @param baseService
	 * @return
	 */
	public static <T extends IPaymentBehavior> IPaymentBehavior getOperationClass(int type,
			IBaseService[] baseService) {

		Class<?> target = defaultPayMap.get(type);

		try {
			Constructor<?> constructor = target.getConstructor(baseService.getClass());
			return (IPaymentBehavior) constructor.newInstance(new Object[] { baseService });

		} catch (Exception e) {
			e.printStackTrace();
			new Error("找不到处理支付状态的类");
		}
		return null;
	}

}
